package com.example.myapplication.wight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：YangLei
 * 时间：2016/5/31
 * 作用说明：
 * 使用说明：
 */
public class ChartView extends View {
    public int XPoint = 40;    //原点的X坐标
    public int YPoint = 50;     //原点的Y坐标
    public int XScale = 80;     //X的刻度长度
    public int YScale = 60;     //Y的刻度长度
    public int XLength = 380;        //X轴的长度
    public int YLength = 240;        //Y轴的长度
    public String[] XLabel;    //X的刻度
    public String[] YLabel;    //Y的刻度
    public ArrayList<ArrayList<String>> totalDataList;      //数据
    public String Title;    //显示的标题
    private DecimalFormat df = new DecimalFormat("0.00");
    private int[] colors = new int[]{Color.WHITE, Color.RED, Color.YELLOW};

    public ChartView(Context context) {
        super(context);
    }

    public ChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setXYLabels(String[] XLabels, String[] YLabels, String strTitle) {
        XLabel = XLabels;
        YLabel = YLabels;
        Title = strTitle;
        invalidate();
    }

    public void refreshData(ArrayList<ArrayList<String>> AllData) {
        totalDataList = AllData;
//        if (totalDataList != null && totalDataList.size() >= 10) {
//            XLabel = new String[totalDataList.size() + 2];
//            for (int i = 0; i < XLabel.length - 1; i++) {
//                XLabel[i] = String.valueOf(i);
//            }
//            XLabel[XLabel.length - 1] = "次";
//            setLayoutParams(new RelativeLayout.LayoutParams(XScale * XLabel.length + 100, RelativeLayout.LayoutParams.MATCH_PARENT));
//        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);//重写onDraw方法
        YPoint = getHeight() - 40;
        XLength = getWidth() - 50;
        YLength = getHeight() - 40;
        //canvas.drawColor(Color.WHITE);//设置背景颜色
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);//去锯齿
        paint.setColor(Color.WHITE);//颜色
        Paint paint1 = new Paint();
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setAntiAlias(true);//去锯齿
        paint1.setColor(Color.WHITE);
        paint.setTextSize(25);  //设置轴文字大小
        //设置Y轴
        canvas.drawLine(XPoint + 40, YPoint - YLength + 10, XPoint + 40, YPoint, paint);   //轴线
        for (int i = 0; i * YScale < YLength; i++) {
            canvas.drawLine(XPoint + 40, YPoint - i * YScale, XPoint + 5 + 40, YPoint - i * YScale, paint);  //刻度
            try {
                canvas.drawText(YLabel[i], XPoint - 22, YPoint - i * YScale + 5, paint);  //文字
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        canvas.drawLine(XPoint + 40, YPoint - YLength + 10, XPoint - 3 + 40, YPoint - YLength + 6 + 10, paint);  //箭头
        canvas.drawLine(XPoint + 40, YPoint - YLength + 10, XPoint + 3 + 40, YPoint - YLength + 6 + 10, paint);
        //设置X轴
        canvas.drawLine(XPoint + 40, YPoint, XPoint + XLength, YPoint, paint);   //轴线
        for (int i = 0; i < XLabel.length; i++) {
            canvas.drawLine(XPoint + i * XScale + 40, YPoint, XPoint + i * XScale + 40, YPoint - 5, paint);  //刻度
            canvas.drawText(XLabel[i], XPoint + i * XScale - 10 + 40, YPoint + 30, paint);  //文字
        }
        if (totalDataList != null) {
            for (int x = 0; x < totalDataList.size(); x++) {
                List<String> resultList = totalDataList.get(x);
                paint.setColor(colors[x % 3]);
                for (int i = 0; i * XScale < XLength; i++) {
                    try {
                        //数据值
                        if (i > 0 && YCoord(resultList.get(i - 1)) != -999 && YCoord(resultList.get(i)) != -999) {  //保证有效数据
                            canvas.drawLine(XPoint + i * XScale + 40, YCoord(resultList.get(i - 1)), XPoint + (i + 1) * XScale + 40, YCoord(resultList.get(i)), paint);
                        }
                        paint.setStyle(Paint.Style.FILL);
                        canvas.drawCircle(XPoint + (i + 1) * XScale + 40, YCoord(resultList.get(i)), 5, paint);
//                        canvas.drawText(
//                                String.valueOf(df.format(Integer.parseInt(resultList.get(i)) / 1024.0)),
//                                XPoint + (i + 1) * XScale + 15,
//                                YCoord(resultList.get(i)) - 15, paint);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        paint.setColor(Color.WHITE);
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6, YPoint - 3, paint);    //箭头
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6, YPoint + 3, paint);
        paint.setTextSize(16);
        if (Title != null) {
            canvas.drawText(Title, 150, 50, paint);
        }
    }

    private int YCoord(String y0)  //计算绘制时的Y坐标，无数据时返回-999
    {
        int y;
        try {
            y = Integer.parseInt(y0);
        } catch (Exception e) {
            return -999;    //出错则返回-999
        }
        try {
            return (int) (YPoint - (y / 1024.0) * YScale / Double.parseDouble(YLabel[1]));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return y;
    }
}
